package com.wgtgt.mysql_init;

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.setting.Setting;

/**
 * MySql创建数据库并初始化数据库SQL脚本文件
 *
 */
public class App {
	public static void main(String[] args) {
		System.out.println("Hello World!");
		Setting setting = new Setting("db.setting");
		String username = setting.getStr("username");
		String password = setting.getStr("password");
		String url = setting.getStr("url");
		String mysqlPath = "\"C:\\Program Files\\MySQL\\MySQL Server 9.2\\bin\\\"";
//        String sqlPath = "C:\\Users\\Administrator\\Desktop\\sql";
		String sqlPath = "C:/sql";
		String driver = setting.getStr("driver");
		String host = StrUtil.subBetween(url, "jdbc:mysql://", ":");
		String port = StrUtil.subBetween(StrUtil.removePrefix(url, "jdbc:mysql://"), ":", "/");
		String sql = "CREATE DATABASE ${database} CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;" + "";
		String database = StrUtil.subBetween(StrUtil.removePrefix(url, "jdbc:mysql://"), "/", "?");
		;
		sql = sql.replace("${database}", database);
		String cmd = "cd " + mysqlPath + " & mysql" + " -h" + host + " -u" + username + " -p" + password + " -P" + port
				+ " -e " + "\"" + sql + "\"";
		System.out.println(cmd);
		String execForStr = RuntimeUtil.execForStr(Charset.forName("GBK"), "cmd ", "/c", cmd);
		System.out.println(execForStr);
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		File folder = new File(sqlPath);
		DataSource dataSource = DSFactory.get();
		Connection connection = null;
		try {
			connection = dataSource.getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		if (folder.exists()) {
			FileFilter fileFilter = createFileFilter();
//			FilenameFilter filenameFilter = createFilenameFilter();
			File[] list = folder.listFiles(fileFilter);
			List<String> listSql = new ArrayList<>();
			for (File file : list) {
				String fileName = file.getAbsolutePath();
				String sqlName = fileName.replace("\\", "/");
				String sql2 = "source " + sqlName + ";";
				System.out.println(sql2);
				listSql.add(sql2);
				String cmd1 = "cd " + mysqlPath + " & mysql" + " -h" + host + " -u" + username + " -p" + password
						+ " -P" + port + " " + database + " -e " + "\"" + sql2 + "\"";
				System.out.println(cmd1);
				String execForStr1 = RuntimeUtil.execForStr(Charset.forName("GBK"), "cmd ", "/c", cmd1);
				System.out.println(execForStr1);
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
//			String[] sql1 = { "" };
//			sql1 = listSql.toArray(sql1);
//			System.out.println(Arrays.toString(sql1));
//			try {
//				SqlExecutor.execute(connection, sql1[0]);
//			} catch (SQLException e) {
//				e.printStackTrace();
//			} finally {
//				if (connection != null) {
//					try {
//						connection.close();
//					} catch (SQLException e) {
//						e.printStackTrace();
//					}
//				}
//			}
		}
	}

	private static FileFilter createFileFilter() {
		return new FileFilter() {

			@Override
			public boolean accept(File file) {
				String name = file.getName();
				return file.isFile() && name.endsWith(".sql");
			}

		};
	}

	private static FilenameFilter createFilenameFilter() {
		return new FilenameFilter() {

			@Override
			public boolean accept(File file, String name) {
				return file.isFile() && name.endsWith(".sql");
			}

		};
	}
}
